<?php
namespace app\admin\controller;
use think\Db;
class Pluginapp extends Base{
    public function pluginapp(){
        $list=Db::name('pluginapp')->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function topay(){
        $id=input('param.id');
        $post = array(
            'url' => $_SERVER['HTTP_HOST'],
            'id' => $id,
        );
        $isurl=$this->httpRequest("http://ind.wihuoban.cn/api/index/pluginorder",$post);
        $isurl=json_decode($isurl);
        if($isurl->code=='501'){
            return json(['code' => 501, 'url' => '', 'msg' => '请给出域名参数']);
        }elseif($isurl->code=='502'){
            return json(['code' => 502, 'url' => '', 'msg' => '不存在或者被禁用了！']);
        }elseif($isurl->code=='503'){
            return json(['code' => 503, 'url' => '', 'msg' => '请给出插件id']);
        }elseif($isurl->code=='201'){
            return json(['code' => 201, 'url' => '', 'msg' => '存在订单！']);
        }elseif($isurl->code=='200'){
            $status = Db::name('pluginapp')->where(array('id'=>$id))->update(['buy_state'=>2]);
            if($status){
                return json(['code' => 200,'msg' => '下单成功']);
            }else{
                return json(['code' => 500,'msg' => '下单状态失败']);
            }
        }elseif($isurl->code=='500'){
            return json(['code' => 500, 'url' => '', 'msg' => '下单失败！']);
        }
    }
    private function httpRequest($url,$data){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}
